package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILSMsgListener;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.updata.UpdataAPK;
import com.longrise.bjjt.util.PrefUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.MsgConstant;
import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.main.RecommendView;

/**
 * 个人信息主界面
 */
public class PersonalInfoView extends LFView implements View.OnClickListener, ILSMsgListener {

    private Context mContext;

    private View mView;

    private LinearLayout myCardLayout;

    private LinearLayout myCarLayout;

    private LinearLayout phoneLayout;

    private TextView phoneTV;

    private TextView logoutTV;

    private BaseApplication application;
    //绑定驾驶证
    private TextView bindCardTV;
    //绑定机动车
    private TextView bindCarTV;

    private EntityBean baseBean;//基础数据

    private EntityBean[] carBeans;//绑定的车辆信息

    private EntityBean cardBean;//驾驶证信息

    private TextView nameTView;

    private TextView carnumTView;

    private LinearLayout articleLayout;

    private LinearLayout feedbackLayout;

    private LinearLayout updateLayout;

    private LinearLayout aboutLayout;

    private LinearLayout shareLayout;

    private LinearLayout snapshotLayout;

    private LinearLayout helpPhoneLayout;

    private UpdataAPK updataAPK;

    public PersonalInfoView(Context context, EntityBean bindBean) {
        super(context);
        this.mContext = context;
        application = ((MainActivity) mContext).getAppData();
        initInfos(bindBean);
    }

    public void setBindBean(EntityBean bindBean) {
        initInfos(bindBean);
        setData();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_personalinfo, null);
        nameTView = (TextView) mView.findViewById(R.id.pinfo_tv_name);
        carnumTView = (TextView) mView.findViewById(R.id.pinfo_tv_carnum);
        myCardLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_bindcard);
        myCarLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_bindcar);
        phoneLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_modifyphone);
        phoneTV = (TextView) mView.findViewById(R.id.pinfo_tv_phone);
        logoutTV = (TextView) mView.findViewById(R.id.pinfo_tv_logout);
        articleLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_article);
        feedbackLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_feedback);
        updateLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_update);
        aboutLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_about);
        shareLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_share);
        snapshotLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_snapshot);
        helpPhoneLayout = (LinearLayout) mView.findViewById(R.id.pinfo_layout_helpphone);
        //绑定驾驶证
        bindCardTV = (TextView) mView.findViewById(R.id.pinfo_tv_bindcard);
        //绑定机动车
        bindCarTV = (TextView) mView.findViewById(R.id.pinfo_tv_bindcar);

        regEvent(true);
        this.addILSMsgListener(this);
        baseBean = application.getLoginInfo().getBean("userinfo");
        setData();
    }

    private void regEvent(boolean b) {
        if (null != myCardLayout) {
            myCardLayout.setOnClickListener(b ? this : null);
        }
        if (null != myCarLayout) {
            myCarLayout.setOnClickListener(b ? this : null);
        }
        if (null != phoneLayout) {
            phoneLayout.setOnClickListener(b ? this : null);
        }
        if (null != logoutTV) {
            logoutTV.setOnClickListener(b ? this : null);
        }
        if (null != articleLayout) {
            articleLayout.setOnClickListener(b ? this : null);
        }
        if (null != feedbackLayout) {
            feedbackLayout.setOnClickListener(b ? this : null);
        }
        if (null != updateLayout) {
            updateLayout.setOnClickListener(b ? this : null);
        }
        if (null != aboutLayout) {
            aboutLayout.setOnClickListener(b ? this : null);
        }
        if (null != shareLayout) {
            shareLayout.setOnClickListener(b ? this : null);
        }
        if (null != snapshotLayout) {
            snapshotLayout.setOnClickListener(b ? this : null);
        }
        if (null != helpPhoneLayout) {
            helpPhoneLayout.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        LFView view = null;
        switch (v.getId()) {
            case R.id.pinfo_layout_bindcard:
                if (null == cardBean) {//绑定驾驶证
                    view = new BindDriveCardView(mContext, null);
                } else {//查看驾驶证
                    view = new MyDriveCardView(mContext, cardBean);
                }
                break;
            case R.id.pinfo_layout_bindcar:
                if (null == carBeans) {//绑定机动车
                    view = new BindCarView(mContext, null);
                } else {//查看车辆信息
                    view = new MyCarView(mContext, carBeans);
                }
                break;
            case R.id.pinfo_layout_modifyphone://修改手机号
                view = new ModifyPhoneView(mContext);
                break;
            case R.id.pinfo_tv_logout://退出登录
                logout();
                break;
            case R.id.pinfo_layout_article://我的收藏
                view = new MyCollectView(mContext);
                break;
            case R.id.pinfo_layout_share://我的分享
                view = new RecommendView(mContext);
                break;
            case R.id.pinfo_layout_feedback://反馈
                view = new FeedBackView(mContext);
                break;
            case R.id.pinfo_layout_update://更新
                checkVersion();
                break;
            case R.id.pinfo_layout_about://关于
                view = new VersionView(mContext);
                break;
            case R.id.pinfo_layout_snapshot://随手拍
                view = new MySnapshotView(mContext);
                break;
            case R.id.pinfo_layout_helpphone://服务热线
                checkHelpPhone();
                break;
            default:
                break;
        }
        if (null != view) {
            FrameworkManager.getInstance().showNewForm(mContext, view);
        }
    }

    //打电话
    private void checkHelpPhone() {
        //拨号界面
        Uri uri = Uri.parse("tel:4001187780");
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        mContext.startActivity(intent);
    }

    /**
     * 检查版本更新
     */
    private void checkVersion() {
        if (updataAPK == null) {
            updataAPK = new UpdataAPK(mContext, mContext.getResources().getString(R.string.update_appname),
                    mContext.getResources().getString(R.string.kckpServerUrl), URLConstant.CHECKVERSION,
                    mContext.getResources().getString(R.string.updatekey));
            updataAPK.setUpdateUrl(((MainActivity) mContext).getAppData().getUpdateUrl());
            updataAPK.setCheckVersionListener(new UpdataAPK.CheckVersionListener() {
                @Override
                public void onCheckVersion(int mark) {
                    DialogUtils.closeDialog();
                    if (mark == 0) {
                        UiUtil.showToast(mContext, "当前已经是最新版本！");
                    }
                }
            });
        }
        DialogUtils.showDialog(mContext);
        updataAPK.checkVersion();
    }

    //解析信息
    private void initInfos(EntityBean bean) {
        if (null == bean) {
            return;
        }
        cardBean = bean.getBean("use");
        carBeans = bean.getBeans("car");
    }

    private void setData() {
        if (null == baseBean) {
            return;
        }
        //姓名
        nameTView.setText(baseBean.getString("name"));
        //车辆总数
        String num = "0";
        if (null != carBeans) {//存在机动车数据
            num = String.valueOf(carBeans.length);
        }
        carnumTView.setText(num);
        //手机号
        String phone = baseBean.getString("mobilephone");
        if (TextUtils.isEmpty(phone)) {
            phone = "";
        }
        phoneTV.setText(phone);

        if (null == cardBean) {
            bindCardTV.setText("未绑定");
            bindCardTV.setTextColor(Color.parseColor("#f2572e"));
        } else {
            String cardno = cardBean.getString("cardno");
            String cardImg = cardBean.getString("drivernoimg");
            String fileno = cardBean.getString("driverfileno");
            if (TextUtils.isEmpty(cardno) || TextUtils.isEmpty(cardImg) || TextUtils.isEmpty(fileno)) {
                //驾驶证信息有缺失
                bindCardTV.setText("待完善");
                bindCardTV.setTextColor(Color.parseColor("#f2572e"));
            } else {
                bindCardTV.setText("已绑定");
                bindCardTV.setTextColor(Color.parseColor("#666666"));
            }
        }
        if (null == carBeans) {
            bindCarTV.setText("未绑定");
            bindCarTV.setTextColor(Color.parseColor("#f2572e"));
        } else {
            EntityBean bean = carBeans[0];
            String carno = bean.getString("carno");
            String cartype = bean.getString("cartype");
            String carImg = bean.getString("drivingnoimg");
            String cationnum = bean.getString("identificationnum");
            if (TextUtils.isEmpty(cationnum) || TextUtils.isEmpty(carImg) || TextUtils.isEmpty(cartype)
                    || TextUtils.isEmpty(carno)) {
                //机动车信息有缺失
                bindCarTV.setText("待完善");
                bindCarTV.setTextColor(Color.parseColor("#f2572e"));
            } else {
                bindCarTV.setText("已绑定");
                bindCarTV.setTextColor(Color.parseColor("#666666"));
            }
        }
    }

    /**
     * 退出登录
     */
    private void logout() {
        UiUtil.showDialog(mContext, "是否退出当前账号？", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 清空sp的账号，密码，token
                PrefUtils.removeStringData(mContext, "loginInfo");
                PrefUtils.removeStringData(mContext, MsgConstant.LOGIN_PHONE);
                PrefUtils.removeStringData(mContext, MsgConstant.USER_TOKEN);
                PrefUtils.removeStringData(mContext, "carassess");
                //清除缓存
                application.setLoginInfo(null);
                application.setToken(null);
                application.setKckpName(null);
                //回到登录页
                FrameworkManager.getInstance().destroyAllForm();

                //进入登录界面
                UiUtil.showToast(mContext, "退出登录成功");
            }
        });
    }

    @Override
    public void refresh() {

    }

    @Override
    public Object onLSMsg(int i, Object... objects) {
        switch (i) {
           /* case 99://返回时刷新界面数据
                bindState();
                break;*/
            case 98://手机信息已修改
                baseBean = application.getLoginInfo().getBean("userinfo");
                setData();
                break;
            case 102://重新获取绑定的数据
                EntityBean bean = (EntityBean) JSONSerializer.getInstance().DeSerialize(PrefUtils.
                        getString(mContext, "carinfo", null), EntityBean.class);
                EntityBean bindData = bean.getBean("data");
                if (null != bindData) {
                    initInfos(bindData);
                    setData();
                }
                break;
        }
        return null;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        regEvent(false);
        removeILSMsgListener(this);
        DialogUtils.destroyDialog();
    }
}
